package com.leetcode.dynamic.d11;

/**
 * 96. 不同的二叉搜索树
 * 难度中等
 * 给你一个整数 n ，求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种？返回满足题意的二叉搜索树的种数。
 * <p>
 * 1 <= n <= 19
 *
 * @author Kar
 * @create 2021-12-07 上午11:42
 */
public class UniqueBinarySearchTrees {
    public static void main(String[] args) {

    }
    //https://leetcode-cn.com/problems/unique-binary-search-trees/solution/bu-tong-de-er-cha-sou-suo-shu-by-leetcode-solution/
    public int numTrees(int n) {
        int[] G = new int[n + 1];
        G[0] = 1;
        G[1] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <=i; j++) {
                G[i] += G[j - 1] * G[i - j];
            }
        }
        return G[n];
    }
}
